const express = require("express");
const router = express.Router();
const { checkPermission } = require("../middleware");

const tablesByRole = {
	"SuperAdmin": ["routes", "userinfo"],
	"Admin": ["anime", "comic", "galgame", "roles", "voice_actor", "authorship"],
	"User": ["article", "comment"],
	"Public": ["my"]
};

// 开放路由登录，注册
const loginRouter = require("./modules/Login");
router.use("/api", loginRouter());

// 动态导入模块路由
const initializeRoutes = (role) => {
	const modules = tablesByRole[role] || [];
	const RoleRouter = require(`./modules/${role}`);

	modules.forEach(moduleName => {
		let permissions;
		if (role === "SuperAdmin") {
			permissions = ["SuperAdmin"];
		} else if (role === "Admin") {
			permissions = ["SuperAdmin", "Admin"];
		} else {
			permissions = ["SuperAdmin", "Admin", "User"];
		}

		router.use(`/${moduleName}`, checkPermission(permissions), RoleRouter(moduleName));
	});
};

// 路由对应角色
["SuperAdmin", "Admin", "User", "Public"].forEach(initializeRoutes);

module.exports = router;
